package com.example.administrator.meishi.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.meishi.R;
import com.example.administrator.meishi.tools.NewsDao;

/**
 * @version V1.0
 * @类名称: LookCollectActivity
 * @类描述: 查看收藏功能
 * @创建人：一一哥
 * @创建时间：2015年9月7日 下午10:17:51
 * @备注：
 */
public class LookCollectActivity extends Activity {

    private ListView mLv;
    private SimpleCursorAdapter adapter;
    private NewsDao dao;
    private String sql;
    private TextView tx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.look_collect_activity);

        mLv = (ListView) findViewById(R.id.lv_collected);

        // 查询数据库.展示收藏结果.
        dao = new NewsDao(this);
        sql = "select * from shen";
        final Cursor cursor = dao.select(sql, null);
        adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"test","path"}, new int[]{R.id.newstext,R.id.path},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        mLv.setAdapter(adapter);

        // 注册上下文菜单
        registerForContextMenu(mLv);

        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String xq = cursor.getString(cursor.getColumnIndex("test"));
                String src = cursor.getString(cursor.getColumnIndex("path"));
                Intent intent = new Intent(LookCollectActivity.this, NewActivity.class);
                intent.putExtra("video",src);
                intent.putExtra("text",xq);
                startActivity(intent);
            }
        });
    }

    // 创建上下文菜单.移除收藏.
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.remove_collect, menu);
    }

    // 实现移除收藏功能
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        // 得到对应位置的Cursor数据.
        ContextMenuInfo menuInfo = item.getMenuInfo();
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        int position = info.position;
        Cursor cursor = (Cursor) adapter.getItem(position);

        // 实现移除功能.从数据库中删除指定收藏条目.
        if (item.getItemId() == R.id.action_remove) {
            int delete = dao.delete("shen", "test=?",
                    new String[]{cursor.getString(cursor
                            .getColumnIndex("test"))});
            cursor.close();
            if (delete > 0) {
                // 重新查询数据库.刷新界面.
                Cursor newCursor = dao.select(sql, null);
                //交换Cursor结果的方法.
                adapter.swapCursor(newCursor);
                Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show();
            }
        }

        return super.onContextItemSelected(item);
    }
}
